Basic C Programming (Part 2)
By Arthur Ed LeBouthillier
This article was published in the January 2000 issue off The Robot Builder.
This is a follow-up on the article: "Basic C Programming (Part 1)"
Last month, we reviewed two important ideas about the C language: that C is a typed language and that C is a functional language. This month, let’s look a C types.
Table 1 shows the variety of C types available in almost any implementation. It is important to realize that not all implementations of C will have all of these types. Additionally different implementations may have different bit sizes assigned for each of the types.
As you can see, types char, unsigned char, short, unsigned short, int, unsigned int, long and unsigned long can represent only whole numbers (numbers without a decimal point). Types float and double can be used to represent decimal numbers
quite accurately. Type void refers to an unspecified type and is often used to specify that no value is returned from a function; it also has other importan uses. Notice that each of the types has limits on the range that it can represent; you must select the
proper type to represent the number you want.
Variables
A variable is a named place to store a value of a specific type. If you think of those hotel mailboxes recently declared variable like this:
a = 5 + 3;
You can also use variables in mathematical formulas like this:
int a;
int b;
b = 3;
a = b +5;
In this example, we declared two variables, a and b, assigned the number 3 to b and then assigned b plus 5 to a. Obviously, after doing these operations, b would contain the value 3 and a would contain the value 8. We could also declare other types, such as char’s which could accept characters. It is worth mentioning how C stores characters. A character is stored as an 8-bit whole number which takes the value according to what is known as ASCII. ASCII is a code system which assigns a number to each of the letters, numbers and other characters used. We could declare a variable c and assign it a character a
in this manner:
char c;
c = ‘a’;
A character is denoted by putting the character in single quotes as above. We could declare a floating point variable f and assign it a value like this:
float f;
f=3.9;
The most important thing to realize about C is that you MUST declare a variable before you use.
Simple Programs
We have talked about the idea that C is a functional language meaning that you write a program by declaring functions. Let’s look at what a simple program might look like. All C programs consist of a function called main which calls the other functions you define. Therefore, the simplest program consists merely of a main function. Here is one of the most common simple programs:
#include “stdio.h”
void main()
{
printf(“Hello world\n”);
}
This program has only one function, main, and inside that function is only one statement:
printf(“Hello world\n”);
The result of running this program is that the line:
Hello world
would appear on the computer screen. The first part of this program is the line:
#include “stdio.h”
This statement, known as an include statement, tells C that you want to use previously defined functions which are declared in the file stdio.h. C allows you to put parts of your program in many different files so that you can reuse often used functions. From a programming standpoint, this allows modularization of your code because you can create files which can be used in a bunch of different programs. Stdio.h is a standard input/output library which defines a whole host of functions which you can use for input and output. printf is a function which is used to print things on the screen.
After we included the functions from the stdio.h file, we then declared the main function. We did this by declaring its type, void, followed by the functions name, main, followed by empty parenthesis pairs followed by what is known as a block. A block is a sequence composed of a left brace, {, with a number of statements separated by semicolons, followed by a right brace, }. The
general rule for declaring a function is:
type name ( arguments ) block
This means you must declare the type of value returned by the function (or void if there is no return value), followed by the name of the function, followed by a left parenthesis, followed by a declaration for the function’s arguments, followed by a right parenthesis, followed by a block. As we said, a block is a left brace, followed by a number of statements, separated by semicolons, followed by a right brace. Taking all of this complicated description of a function declaration, we derive the
main function:
void main ()
{
printf(“Hello world\n”);
}
The function’s block contains only one statement in it, printf(“Hello world\n”). There were no arguments into the function so there is nothing between the parentheses.
We could have done other things within the main function. A different program using the variable declarations above is:
#include “stdio.h”
void main()
{
int a;
int b;
a = 3;
b = a + 10;
printf(“a = %d\n”, a);
printf(“b = %d\n”, b);
}
This program creates variables a, and b, stores some numbers in each of them and then prints their values to the screen.
We could have had our main function call another function that we declared like this:
#include “stdio.h”
int sum(int a, int b)
{
return a + b;
}
void main()
{
int c;
int d;
int e;
c = 5;
d = 6;
e = sum( c, d );
printf(“e = %d\n”,e);
}
This program declares two functions: sum which returns an integer type and main which returns no value. Sum takes two arguments, a and b which are of type int, adds them together and returns their sum as a type int. Main declares three variables, c, d and e, assigns values to c and d and then calls the function sum with their values. The result of the sum function is assigned to e and then this result is printed on the screen. Although somewhat complicated, this is representative of most programs you’ll write: you define one or more auxiliary functions which are called from main. Together, these functions perform the operations you want your program to perform.
Summary
We looked at the types of variables one can declare in C. We also looked at a few complete programs in C that print results on the screen. There’s a lot more to learn and we’ll look at some new more complicated functions next month
The follow-up to this article is: "Basic C Programming (Part 3)"